import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'

import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,parseToChart} from '../apis/core-util'
import basicInfo from '../apis/basicInfo'

export default class MainScope extends Scope {
  constructor(props){
    super();
    this.osId = props.routeParams.osId
    this.osversionId = props.routeParams.osversionId
    this.$state = {
      title:'',
      chart0:{
        loading:true
      },
      chart1:{
        loading:true
      },
      chart2:{
        loading:true
      },
      chart3:{
        loading:true
      }
    }
    if(this.osversionId){
      this.basicInfo = basicInfo({
        id:this.osversionId,
        parentId:this.osId,
        urlName:'os'
      })
    }else if(this.osId){
      this.basicInfo = basicInfo({
        id:this.osId,
        urlName:'os'
      })
    }
    this.chart0Api = coreApi({
      limit:5,
      classify:'osversion',
      viewKey:'avg_exec',
      urlName:'interaction'
    },TimeStore.getValue,function (){
      return {order:sortMap[this.viewKey]}
    },function(){
      if(this.osversionId){
        return {
          osVersionId:this.osversionId,
          osId:this.osId
        }
      }else if(this.osId){
        return {
          osId:this.osId
        }
      }
    }.bind(this));
    this.chart1Api = coreApi({
      limit:5,
      classify:'osversion',
      classifyKeyField:'time',
      classifyCountField:'num',
      viewKey:'avg_resp',
      urlName:'httptransaction'
    },TimeStore.getValue,function (){
      return {order:sortMap[this.viewKey]}
    },function(){
      if(this.osversionId){
        return {
          osVersionId:this.osversionId,
          osId:this.osId
        }
      }else if(this.osId){
        return {
          osId:this.osId
        }
      }
    }.bind(this));
    this.chart2Api = coreApi({
      limit:5,
      classify:'osversion',
      viewKey:'error_rate',
      urlName:'errorrate'
    },TimeStore.getValue,function (){
      return {order:sortMap[this.viewKey]}
    },function(){
      if(this.osversionId){
        return {
          osVersionId:this.osversionId,
          osId:this.osId
        }
      }else if(this.osId){
        return {
          osId:this.osId
        }
      }
    }.bind(this));
    this.chart3Api = coreApi({
      limit:5,
      classify:'osversion',
      viewKey:'session_count',
      urlName:'session'
    },TimeStore.getValue,function (){
      return {order:sortMap[this.viewKey]}
    },function(){
      if(this.osversionId){
        return {
          osVersionId:this.osversionId,
          osId:this.osId
        }
      }else if(this.osId){
        return {
          osId:this.osId
        }
      }
    }.bind(this));
  }
  fetch(){
    var _this = this
    this.chart0Api.fetch().then(function(data,params){
      _this.$state.chart0 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart0 = {
        error:err
      }
    },function(params){
      _this.$state.chart0 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart1Api.fetch().then(function(data,params){
      _this.$state.chart1 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart1 = {
        error:err
      }
    },function(params){
      _this.$state.chart1 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart2Api.fetch().then(function(data,params){
      _this.$state.chart2 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart2 = {
        error:err
      }
    },function(params){
      _this.$state.chart2 = {
        loading:true,
        params:params
      }
    }).always(function(){
      _this.emit()
    })
    this.chart3Api.fetch().then(function(data,params){
      _this.$state.chart3 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart3 = {
        error:err
      }
    },function(params){
      _this.$state.chart3 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
  }
  $settime(payload){
    this.fetch()
  }
  $unload(){
  }
  $load(){
    var _this = this
    this.basicInfo.fetch().done(function(data){
      //TODO 当basicInfo查不到时应显示错误,可以在Router.transtition时加载basicInfo并判断
      _this.$state.title = data.result[0].name;
      _this.emit()
    })
    this.fetch();

  }
}
